<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


class SendPhoneUser extends Base
{
    protected $table = 'send_phone_user';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /*
    * 获取用户点击资格
     * @param [user_id] 用户id
     * @param [rank] 充值等级  递增修改
     * @param [type] 第几次活动
     * @param [status] 0 未充值 1 已充值
    * */
    public function verifyUser($user_id, $rank = 0, $type = 1)
    {
        $w['user_id'] = $user_id;
        $w['type'] = $type;
        $db = DB::table($this->table)->where($w)->first();
        if ($db) {
            if ($db->status == 0) return 201;//未充值
            if ($db->is_draw == 1) return 202;//已经领取
            if ($db->rank < $rank) return 203; //领取等级不符合
            return 204;
        }
        if (!$db) {
            $data['user_id'] = $user_id;
            $data['rank'] = 0;
            $data['type'] = $type;
            $data['status'] = 0;
            $data['created_at'] = $data['updated_at'] = date_ymd();
            DB::table($this->table)->insert($data);
        }
        return 200; // 首次点击
    }


    public function checkUser($user_id,$phone){
    //符合领取规则 查看是否领取
        $user = "SENDPHONE:" . $user_id;
        $hashUserAll = Redis::hgetall($user);
        if (empty($hashUserAll)) {
            return 209; // 未领取过手机号码
        }
        if($hashUserAll['phone'] == $phone){
            return 205;// 该号码为当前用户抢购号码
        }else{
            $res['phone'] = $hashUserAll['phone'];
            $res['code'] = 208;
            return $res; //已存在领取手机号
        }

    }

    public function getUser($user_id, $date = [], $rank = 1, $type = 1)
    {
        $w['user_id'] = $user_id;
        $w['is_draw'] = 0;
        $w['type'] = $type;
        return DB::table($this->table)
            ->where($w)
            ->where('rank', '<', $rank)
            ->whereBetween('created_at', $date)
            ->first();
    }

    public function edit($user_id, $rank = 1, $is_draw = 0)
    {
        $w['user_id'] = $user_id;
        if ($is_draw == 0) {
            $data['rank'] = $rank;
            $data['status'] = 1;
        } else {
            $data['is_draw'] = $is_draw;
        }
        $data['updated_at'] = date_ymd();
        return DB::table($this->table)->where($w)->update($data);
    }

    public function getUserInfo($user_id, $rank = 1, $type = 1)
    {
        $w['user_id'] = $user_id;
        $w['is_draw'] = 0;
        $w['type'] = $type;
        return DB::table($this->table)
            ->where($w)
            ->where('rank', '>=', $rank)
            ->first();
    }

    public function getUserRank($user_id, $type = 1)
    {
        $w['user_id'] = $user_id;
        $w['is_draw'] = 0;
        $w['type'] = $type;
        return DB::table($this->table)
            ->where($w)
            ->first();
    }


}